import {Component, OnInit} from '@angular/core';
import { HttpService } from '../http.service';
import { UserService } from '../user.service';
import {loadMore} from '../../assets/js/utils';


@Component({selector: 'app-friends', templateUrl: './friends.component.html', styleUrls: ['./friends.component.less']})
export class FriendsComponent implements OnInit {
  order : string = 'time';
  loading: boolean = true;
  non_directer;
  agent_profit_sum;
  agent_direct_profit_sum;
  direct_profit_sum;
  list = [];
  page = 1;

  loadMore = new loadMore({cb: this.nextPage.bind(this)});

  constructor(
    private http: HttpService,
    private user: UserService,
  ) {
    this.getData();
  }

  clickOrder(e, order) : void {
    this.order = order
    this.page = 1
    console.log('关闭下拉加载')
    this.loadMore.close()
    this.list = []
    this.getData()
  }

  async nextPage(){
    this.page++
    await this.getData()
  }

  async getData(){
    const {success, data, msg} = await this.http.post({
      name: 'car.get.friend.page',
      type: this.order === 'time' ? 1 : 2,
      page: this.page
    })
    this.loadMore.loading = false
    this.loading = false;
    this.non_directer = data.non_directer;
    this.agent_profit_sum = data.agent_profit_sum;
    this.agent_direct_profit_sum = data.agent_direct_profit_sum;
    this.direct_profit_sum = data.direct_profit_sum;

    const list = data.profit_details;
    if(Array.isArray(list)) this.list.push(...list)
    setTimeout(()=>{
      if(document.body.scrollHeight > window.innerHeight && this.page == 1)
        this.loadMore.open()
      else if(!list.length)
        this.loadMore.close()
      }, 100)
  }

  ngOnInit() {}

}
